//
//  PreferenceController.m
//  <6.1>RaiseMan
//
//  Created by chen on 16/9/12.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "PreferenceController.h"

 NSString *const  BNRTableBgColorKey  = @"BNRTableBgColor";
 NSString *const  BNREmptyDocKey = @"BNREmptyDocFlag";
 NSString *const  BNRBackgroudColorChangedNotification = @"BNRBackgroudColorChanged";


@interface PreferenceController ()

@property (weak) IBOutlet NSColorWell *colorWell;

@property (weak) IBOutlet NSButton *checkBox;

@end

@implementation PreferenceController

- (void)windowDidLoad {
    [super windowDidLoad];
    
    [_colorWell setColor:[PreferenceController preferenceTableBgColor]];
    [_checkBox  setState:[PreferenceController preferenceEmptyDoc]];
    
}

- (instancetype)init{
    //这样在需要时就会自动加载
    self = [super initWithWindowNibName:@"Preference"];
    return  self ;
}



- (IBAction)changeBackgroudColor:(id)sender{
 
    NSColor *color = [_colorWell color];
    NSLog(@"color = %@",color);
    [PreferenceController setPreferenceTableBgColor:color];
    
    //发送通知 
    //[[NSNotificationCenter defaultCenter]postNotificationName:BNRBackgroudColorChangedNotification object:self];
    [[NSNotificationCenter defaultCenter]postNotificationName:BNRBackgroudColorChangedNotification object:nil userInfo:@{@"color":color}];
}


- (IBAction)changeNewEmptyDoc:(id)sender{
    
    NSInteger state = [sender state];
    NSLog(@"state = %ld", state);
    [PreferenceController setPreferenceEmptyDoc:state];
}



///....

+ (NSColor *)preferenceTableBgColor
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSData *colorAsData = [ud objectForKey:BNRTableBgColorKey];
    return [NSKeyedUnarchiver unarchiveObjectWithData:colorAsData];
}

+ (void)setPreferenceTableBgColor:(NSColor *)color
{
    NSData *colorAsData = [NSKeyedArchiver archivedDataWithRootObject:color];
    [[NSUserDefaults standardUserDefaults]setObject:colorAsData forKey:BNRTableBgColorKey];
}

+ (BOOL)preferenceEmptyDoc
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:BNREmptyDocKey];
}

+ (void)setPreferenceEmptyDoc:(BOOL)emptyDoc
{
    [[NSUserDefaults standardUserDefaults]setObject:@(emptyDoc) forKey:BNREmptyDocKey];
}


@end
